<?php


namespace addons\Minerva\api\modules\v1\controllers;

use addons\Minerva\common\models\forms\LoginForm;
use Yii;
use common\helpers\ResultHelper;
use api\controllers\OnAuthController;
use yii\web\NotFoundHttpException;

class SiteController extends OnAuthController
{
    public $modelClass = '';

    /**
     * 不用进行登录验证的方法
     *
     * 例如： ['index', 'update', 'create', 'view', 'delete']
     * 默认全部需要验证
     *
     * @var array
     */
    protected $authOptional = ['login', 'refresh', 'mobile-login', 'sms-code', 'register', 'up-pwd'];

    /**
     * 登录根据用户信息返回accessToken
     *
     * @return array|bool
     * @throws NotFoundHttpException
     * @throws \yii\base\Exception
     */
    public function actionLogin()
    {
        $model = new LoginForm();
        $model->attributes = Yii::$app->request->post();
        if ($model->validate()) {
//            return $model->getUser();
            $data = Yii::$app->minervaService->apiAccessToken->getAccessToken($model->getUser(), $model->group);
            Yii::$app->minervaService->actionLog->create('login', '账号登录', false,$data['member']['id']);
            return $data;
        }

        // 返回数据验证失败
        return ResultHelper::json(422, $this->getError($model));
    }
}